//
//  ForgetPwdViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/8.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "ForgetPwdViewModel.h"

@interface ForgetPwdViewModel ()

/// 验证码按钮点击执行的命令
@property (nonatomic, readwrite, strong) RACCommand *verCodeCommand;
/// 重置密码按钮点击执行的命令
@property (nonatomic, readwrite, strong) RACCommand *resetPwdCommand;

@end

@implementation ForgetPwdViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self)
    
    /// 注册命令
    self.resetPwdCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        
        if (![NSString mh_isValidMobile:self.strPhone])
        {
            return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:@"请输入正确的手机号码"}]];
        }
        if (self.strPwd.length <= 5 && self.strPwd.length > 12)
        {
            return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:@"旧密码填写有误"}]];
        }
        if (![NSString mh_isPureDigitCharacters:self.strVercode] || self.strVercode.length != 6 || ![self.strVercode isEqualToString:self.contrastVerCode])
        {
            return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:@"验证码错误"}]];
        }
        
        return [self requestMobileResetPwdSignal];
    }];
    
    self.verCodeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        
        DataManager* dm = [DataManager sharedDataManager];
        
        if (dm.phoneVCodeCaler.verCodeTime > 0)
        {
            return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:[NSString stringWithFormat:@"请%d秒后再试", dm.phoneVCodeCaler.verCodeTime]}]];
        }
        
        if (![NSString mh_isValidMobile:self.strPhone]) {
            
            return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:@"请输入正确的手机号码"}]];
        }
        
        return [self requestVercodeSignal];
    }];
}

#pragma mark - Request
- (RACSignal*)requestMobileResetPwdSignal
{
    @weakify(self)
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self)
        
        @weakify(self)
        /// 发起请求
        [[AFNetWorkUtils racPOSTWthURL:url_interface_passwordset params:interface_param_passwordset(self.strPhone, self.strPwd)] subscribeNext:^(id x)
         {
             @strongify(self)
             
             NSString* strShowSucTip = [x[SUViewModelTitleKey] isNotBlank] ? x[SUViewModelTitleKey] : @"修改密码成功";
             
             [subscriber sendNext:strShowSucTip];
             [subscriber sendCompleted];
         }   error:^(NSError *error) {
             //错误处理
             
             NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"修改密码失败";
             [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

- (RACSignal*)requestVercodeSignal
{
    @weakify(self)
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self)
        
        DataManager* dm = [DataManager sharedDataManager];
        [dm.phoneVCodeCaler waitGetCode];
        
        @weakify(self)
        /// 发起请求
        [[AFNetWorkUtils racPOSTWthURL:url_interface_sendMessage params:interface_param_sendMessage(self.strPhone)] subscribeNext:^(id x) {
            //成功返回(result class is NSDictionary)
            
            @strongify(self);
            
            self.contrastVerCode = x[@"code"];
            
            [subscriber sendNext:x[SUCommandSysMessageKey]];
            [subscriber sendCompleted];
        } error:^(NSError *error) {
            //错误处理
            
            NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"请求验证码失败，请稍后再试";
            [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
}

@end
